$(document).ready(function() {




	//市調狀態
	$( "#obj_mark" )
	  .bind( 'focus', function(){ 

	  	$(this).autocomplete("search"); 

	  }).bind( 'click', function(){ 

	  	$(this).autocomplete("search"); 
	  	
	  } )
	  .autocomplete({
		minLength: 0,
		source: function(request, response ) {
			//alert(request.term);
	                $.ajax({
	                    url: "/object/market_josn",
	                    dataType: "json",
	                    data:{
						query: request.term
	                    },
	                    success: function( data ) {

	                        response( $.map( data, function( item ) {
	                        return {
								label: item.value,
								value: item.value
	                        }
	                        }));
	                    }
	                });
	    },
		focus: function() {
		  return false;
		},
		select: function( event, ui ) {
			 var terms=new Array();
			 if(this.value!="")
			   terms = this.value.split("/ ");
			 terms.pop();
			 terms.push( ui.item.value );
			 terms.push( "" );
			 this.value = terms.join( "/ " );
			 return false;
		}
	  });

	  
	
		$( "#owner_client" ).autocomplete({
            source: function(request, response ) {
                $.ajax({
                    url: "/client/client_contact",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
							
                        response( $.map( data, function( item ) {
	                        return {																				
								value: item.name,
								mobile: item.mobile,
								home_phone: item.home_phone,
								email: item.email,
							    verify_code : item.ref_client_verify_code,								
	                        }
                        }));
                    }
                });
            },
			select: function(e, ui) {
						
				$("#client_verify_code").val(ui.item.verify_code);
				$("#owner_mobile").val(ui.item.mobile).attr("readonly","true").css("background","aliceblue");
				$("#owner_email").val(ui.item.email).attr("readonly","true").css("background","aliceblue");
				$("#owner_phone_type").val(1);
				$("#owner_phone").val(ui.item.home_phone).attr("readonly","true").css("background","aliceblue");
				
			  	    	
			}
 		});





	$("#contact_name").autocomplete({
            source: function(request, response ) {

                $.ajax({
                    url: "/client/get_client_contact_json",
                    dataType: "json",
                    data:{
						query: $("#client_verify_code").val()
                    },
                    success: function( data ) {

                        response( $.map( data, function( item ) {
	                        return {																				
								value: item.name,
								mobile: item.mobile,
								home_phone: item.home_phone,
								email: item.email,
							    client_contact_id:item.client_contact_id
	                        }
                        }));

                    }
                });
            },
			select: function(e, ui) {
				

				$("#client_content_id").val(ui.item.client_contact_id);
				$("#contact_mobile").val(ui.item.mobile).attr("readonly","true").css("background","aliceblue");
				$("#contact_email").val(ui.item.email).attr("readonly","true").css("background","aliceblue");
				$("#contact_phone_type").val(1);
				$("#contact_phone_info").val(ui.item.home_phone).attr("readonly","true").css("background","aliceblue");
				
			  	    				    	
			}
 		});



	$("#owner_phone_type").change(function(){
		
		if($( "#owner" ).val()!=''){

			var owner_val = $( "#owner" ).val();

			$.ajax({
                    url: "/client/object_client",
                    dataType: "json",
                    data:{
						query: owner_val
                    },
                    success: function( data ) {

                        response( 
					

							$.map( data, function( item ) {

								var temp_type =$("#owner_phone_type").val();
								var temp_home 	=item.home_phone;
								var temp_company =item.company_phone;
								if(temp_type==1){
									$("#owner_phone").val(temp_home);
								}

								if(temp_type==2){
									$("#owner_phone").val(temp_company);
								}
								
								

		                        
                         	})
						);
					
					
                    }

                });


		}

	});


	$("#principal_phone_type").change(function(){
		
		if($( "#principal" ).val()!=''){

			var owner_val = $( "#principal" ).val();

			//alert(owner_val);

			$.ajax({
                    url: "/client/object_client",
                    dataType: "json",
                    data:{
						query: owner_val
                    },
                    success: function( data ) {

                        response( 
					

							$.map( data, function( item ) {

								var temp_type1 	= $("#principal_phone_type").val();
								var temp_home1 	= item.home_phone;
								var temp_company1 =item.company_phone;
									
								/*	
								alert(										
										temp_type1 	+" , "+ 
										temp_home1 	+" , "+
										temp_company1
									);
								*/
								
								if(temp_type1==1){
									$("#principal_phone").val(temp_home1);
								}

								if(temp_type1==2){
									$("#principal_phone").val(temp_company1);
								}
								
								

		                        
                         	})
						);
					
					
                    }

                });


		}

	});


	$( "#owner" ).autocomplete({
            source: function(request, response ) {
                $.ajax({
                    url: "/client/object_client",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {

						//principal_phone_type

						$("#owner_phone_type").val(1);
						$("#principal_phone_type").val(1);
						
                        response( $.map( data, function( item ) {
	                        return {
								label: item.name+"/"+item.recommended,
								value: item.name,
								client_id:item.client_id,
								name:item.name,
								mobile: item.mobile,
								home_phone: item.home_phone
	                        }
                        }));
                    }
                });
            },
			select: function(e, ui) {
			  //$("input:[name='owner_phone_type']").val(1);		
			  $("#client_id").val(ui.item.client_id);
			  $("#owner_mobile").val(ui.item.mobile);
			  $("#owner_phone").val(ui.item.home_phone);
			  $("#principal").val(ui.item.name);
			  $("#principal_mobile").val(ui.item.mobile);
			  $("#principal_phone").val(ui.item.home_phone);
			},
            minLength: 1
 	});



	$( "#environmental_features" )
	  .bind( 'focus', function(){ $(this).autocomplete("search"); } ).bind( 'click', function(){ $(this).autocomplete("search"); } )
	  .autocomplete({
		minLength: 0,
		source: function(request, response ) {
	                $.ajax({
	                    url: "/object/environmental_features_josn",
	                    dataType: "json",
	                    data:{
						query: request.term
	                    },
	                    success: function( data ) {
	                        response( $.map( data, function( item ) {
	                        return {
						label: item.value,
						value: item.value
	                        }
	                        }));
	                    }
	                });
	    },
		focus: function() {
		  return false;
		},
		select: function( event, ui ) {
			 var terms=new Array();
			 if(this.value!="")
			   terms = this.value.split("/ ");
			 terms.pop();
			 terms.push( ui.item.value );
			 terms.push( "" );
			 this.value = terms.join( "/ " );
			 return false;
		}
	  });


	  $( "#building_name" )
      .bind( 'focus', function(){ $(this).autocomplete("search"); } ).bind( 'click', function(){ $(this).autocomplete("search"); } )
      .autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/building_name/building_name_not_in_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        return {
						label: item.value,
						value: item.value
                        }
                        }));
                    }
                });
	    },
        focus: function() {
          return false;
        }
      });


	$( "#case_name" )
      //.bind( 'focus', function(){ 
      //	$(this).autocomplete("search"); 
      //} )
      .bind( 'click', function(){ 
      	$(this).autocomplete("search"); 
      } )
      .autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/object/case_name_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        return {
						label: item.value,
						value: item.value
                        }
                        }));
                    }
                });
	    },
        focus: function() {
          return false;
        }
      });


	  $( "#object_features" )
	  .bind( 'focus', function(){ $(this).autocomplete("search"); } ).bind( 'click', function(){ $(this).autocomplete("search"); } )
	  .autocomplete({
		minLength: 0,
		source: function(request, response ) {
	                $.ajax({
	                    url: "/object/object_features_josn",
	                    dataType: "json",
	                    data:{
						query: request.term
	                    },
	                    success: function( data ) {
	                        response( $.map( data, function( item ) {
	                        return {
						label: item.value,
						value: item.value
	                        }
	                        }));
	                    }
	                });
	    },
		focus: function() {
		  return false;
		},
		select: function( event, ui ) {
			 var terms=new Array();
			 if(this.value!="")
			   terms = this.value.split("/ ");
			 terms.pop();
			 terms.push( ui.item.value );
			 terms.push( "" );
			 this.value = terms.join( "/ " );
			 return false;
		}
	  });


	$( "#public_facilities" )
	  .bind( 'focus', function(){ $(this).autocomplete("search"); } ).bind( 'click', function(){ $(this).autocomplete("search"); } )
	  .autocomplete({
		minLength: 0,
		source: function(request, response ) {
	                $.ajax({
	                    url: "/object/public_facilities_josn",
	                    dataType: "json",
	                    data:{
						query: request.term
	                    },
	                    success: function( data ) {
	                        response( $.map( data, function( item ) {
	                        return {
						label: item.value,
						value: item.value
	                        }
	                        }));
	                    }
	                });
	    },
		focus: function() {
		  return false;
		},
		select: function( event, ui ) {
			 var terms=new Array();
			 if(this.value!="")
			   terms = this.value.split("/ ");
			 terms.pop();
			 terms.push( ui.item.value );
			 terms.push( "" );
			 this.value = terms.join( "/ " );
			 return false;
		}
	  });

	  
	  $( "#fire_and_security" )
	  .bind( 'focus', function(){ $(this).autocomplete("search"); } ).bind( 'click', function(){ $(this).autocomplete("search"); } )
	  .autocomplete({
		minLength: 0,
		source: function(request, response ) {
	                $.ajax({
	                    url: "/object/fire_and_security_josn",
	                    dataType: "json",
	                    data:{
						query: request.term
	                    },
	                    success: function( data ) {
	                        response( $.map( data, function( item ) {
	                        return {
						label: item.value,
						value: item.value
	                        }
	                        }));
	                    }
	                });
	    },
		focus: function() {
		  return false;
		},
		select: function( event, ui ) {
			 var terms=new Array();
			 if(this.value!="")
			   terms = this.value.split("/ ");
			 terms.pop();
			 terms.push( ui.item.value );
			 terms.push( "" );
			 this.value = terms.join( "/ " );
			 return false;
		}
	  });
	$( "#appraisal_agencies" )
	  .bind( 'focus', function(){ $(this).autocomplete("search"); } ).bind( 'click', function(){ $(this).autocomplete("search"); } )
	  .autocomplete({
		minLength: 0,
		source: function(request, response ) {
	                $.ajax({
	                    url: "/object/appraisal_agencies_josn",
	                    dataType: "json",
	                    data:{
						query: request.term
	                    },
	                    success: function( data ) {
	                        response( $.map( data, function( item ) {
	                        return {
						label: item.value,
						value: item.value
	                        }
	                        }));
	                    }
	                });
	    },
		focus: function() {
		  return false;
		},
		select: function( event, ui ) {
			 var terms=new Array();
			 if(this.value!="")
			   terms = this.value.split("/ ");
			 terms.pop();
			 terms.push( ui.item.value );
			 terms.push( "" );
			 this.value = terms.join( "/ " );
			 return false;
		}
	  });
	  
	  
$( "#rent_ancillary" ).bind( 'focus', function(){
	 
	  $(this).autocomplete("search"); 
	}).bind( 'click', function(){ 
		$(this).autocomplete("search"); 
	}).autocomplete({
		minLength: 0,
		source: function(request, response ) {
	                $.ajax({
	                    url: "/object/rent_ancillary_josn",
	                    dataType: "json",
	                    data:{
						query: request.term
	                    },
	                    success: function( data ) {
	                        response( $.map( data, function( item ) {
	                        return {
						label: item.value,
						value: item.value
	                        }
	                        }));
	                    }
	                });
	    },
		focus: function() {
		  return false;
		}		
		,
		select: function( event, ui ) {
			 var terms=new Array();
			 if(this.value!="")
			   terms = this.value.split("/ ");
			 terms.pop();
			 terms.push( ui.item.value );
			 terms.push( "" );
			 this.value = terms.join( "/ " );
			 return false;
		}
		
	  });
	  
	$( "#head" ).autocomplete({
            source: function(request, response ) {
                $.ajax({
                    url: "/client/users_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
	                        return {
								label: item.name,
								value: item.name
	                        }
                        }));
                    }
                });
            },
            minLength: 1
 	});

	$( "#original_broker" ).autocomplete({
            source: function(request, response ) {
                $.ajax({
                    url: "/client/users_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
	                        return {
								label: item.name,
								value: item.name
	                        }
                        }));
                    }
                });
            },
            minLength: 1
 	});

	$( "#broker" ).autocomplete({
            source: function(request, response ) {
                $.ajax({
                    url: "/client/users_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
	                        return {
								label: item.name,
								value: item.name
	                        }
                        }));
                    }
                });
            },
            minLength: 1
 	});	
			
	$("input").keypress(function (e) {
		var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
		if (keyCode == 13){
			var i;
			for (i = 0; i < this.form.elements.length; i++){
				if (this == this.form.elements[i])
					break;
			}
			i = (i + 1) % this.form.elements.length;
			this.form.elements[i].focus();
			return false;
		}else{
			return true;
		}
	});

	$("select").keypress(function (e) {
		var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
		if (keyCode == 13){
			var i;
			for (i = 0; i < this.form.elements.length; i++){
				if (this == this.form.elements[i])
					break;
			}
			i = (i + 1) % this.form.elements.length;
			this.form.elements[i].focus();
			return false;
		}else{
			return true;
		}
	});

});


function statistics_year_of_construction() {
	var ymd = $("#building_completion_date").val().split('/');
	var d = new Date(ymd[0], ymd[1], ymd[2]);
	var myDate = new Date();
	var interval = parseInt(myDate.getTime() - d.getTime());
	var day = parseInt(interval / (365 * 24 * 60 * 60 * 1000) + 1);
	$("#year_of_construction").val(day);
}